<template>
<div class="app-container">
  <el-row :gutter="10" class="mb8">
    <el-col :span="1.5">
      <el-button
          plain
          @click="handleAdd"
      >新增</el-button>
    </el-col>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  </el-row>

  <vxe-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
    <vxe-table-column title="编号" field="number"></vxe-table-column>
    <vxe-table-column title="标题" field="title"></vxe-table-column>
    <vxe-table-column title="数量" field="num"></vxe-table-column>
    <vxe-table-column title="处理人" field="userName"></vxe-table-column>
    <vxe-table-column title="备注" field="remark"></vxe-table-column>
    <vxe-table-column title="状态" align="center" field="state">
      <template #default="scope">
        <dict-tag :options="asset_inventory_state" :value="scope.row.state" />
      </template>
    </vxe-table-column>
    <vxe-table-column title="操作" fixed="right" align="center">
      <template  #default="scope">
        
        <el-button style="margin-right: 10px;" link type="primary"
            size="small" v-if="scope.row.state !== 2"
            @click="handleOperate(scope.row)"
        >盘点</el-button>
        <el-button v-if="scope.row.state !== 2"
            size="small" link type="primary"
            @click="handleDelete(scope.row)"
        >删除</el-button>
        
      </template>
    </vxe-table-column>
  </vxe-table>
  <div class="pagination-container ">
  <pagination
    v-show="total>0"
    :total="total"
    :page.sync="paging.pageNum"
    :limit.sync="paging.pageSize"
    @pagination="onPageChange"
  />
  </div>
  <edit ref="edit" @sub="onSub"></edit>
</div>
    
  </template>
  
  <script>
  import api from "./api.js";
  import { getCurrentInstance} from 'vue'
  import Edit from './edit.vue'
  import eventbus from "@/utils/eventbus.js";
  
  export default {
    components: {
      Edit
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        paging: {
          pageNum: 1,
          pageSize: 10,
        },
        // 查询参数
        queryParams: {
          configName: null,
          configDesc: null,
          configType: null,
          orderType: null,
          status: null,
          createUserId: null,
          updateUserId: null
        },
        // 表单参数
        form: {},
      };
    },
    created() {
      this.getList();
    },
    mounted() {
      eventbus.subscribe('after-inventory', () => {
        this.getList();
      })
    },
    destroyed() {
      eventbus.unsubscribe('after-inventory');
    },
    setup() {
      const { proxy } = getCurrentInstance();
      const { asset_inventory_state } = proxy.useDict("asset_inventory_state");
      return {
        asset_inventory_state
      }
    },
    methods: {
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        const {pageNum, pageSize} = this.paging;

        const query = {...this.queryParams};
        const pageReq = {page: pageNum - 1, size: pageSize};
        api.list(query, pageReq).then(response => {
          const { content, totalElements } = response
          this.list = content;
          this.total = totalElements;
          this.loading = false;
        });
      },
      onPageChange({page, limit}) {

        this.paging.pageNum = page;
        this.paging.pageSize = limit;
        this.getList();
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          configName: null,
          configDesc: null,
          configType: null,
          orderType: null,
          status: 0,
          createTime: null,
          updateTime: null,
          createUserId: null,
          updateUserId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.edit.open({
          actionType: 'add'
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.$refs.edit.open({
          actionType: 'edit',
          row: row
        });
      },
      handleOperate(row) {
        this.$router.push({
          path: `/consume/inventoryCheck/detail/${row.id}`
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$modal.confirm('是否确认删除').then(function() {

          return api.del(row.id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      onSub() {

        this.getList();
      }
    }
  };
  </script>
  
<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 92vh; 
  overflow: hidden; 
}

.vxe-table {
  flex: 1;
  overflow: auto;
}

.pagination-container {
  display: flex;
  justify-content: flex-end; 

}
</style>

